package cn.springboot.chapter04upload.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class CustomExceptionHandler {
    /*@ExceptionHandler(MaxUploadSizeExceededException.class)
    public  void uploadException1(MaxUploadSizeExceededException e, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.write("上传文件大小超出限制");
        out.flush();
        out.close();
    }*/
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView uploadException2(MaxUploadSizeExceededException e, HttpServletResponse response) throws IOException {
        {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "上传文件大小超出限制");
            mv.setViewName("error");
            return mv;
        }
    }
}
